package com.best.onegous.details;


import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.BaseActivity;
import com.best.onegous.R;
import com.best.onegous.Rewrite.SlideShowViews;
import com.best.onegous.View.DetailsListview;
import com.best.onegous.View.DetailsListview.MyOnScroll;
import com.best.onegous.View.PullPushLayout;
import com.best.onegous.View.PullPushLayout.OnTouchEventMoveListenre;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.http.BusinessResponse;
import com.best.onegous.http.HttpClients;
import com.loopj.android.http.RequestParams;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class YiJieXiao extends BaseActivity implements OnClickListener,MyOnScroll,BusinessResponse{
	private DetailsListview listview;
	private View bannerView;
	private RelativeLayout rl_title;
	private TextView title,name,content,qishu,zongxu,jindu,xingyunma,zhongjiangzhe,huafei;
	private int headerHeight;
	private SlideShowViews slideshowView;
	private PullPushLayout mLayout;
	private int alphaMax = 180;
	private Intent intent;
	private String id,codeGoodsID="",codePeriod;
	private HttpClients httpclient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setThem(R.layout.activity_yijiexiao);
		intent = getIntent();
		id = intent.getStringExtra("id");
		findViewById(R.id.back).setOnClickListener(this);
		slideshowView = (SlideShowViews) findViewById(R.id.slideshowView);
		
		rl_title = (RelativeLayout) findViewById(R.id.top);
		rl_title.getBackground().setAlpha(0);
		title = (TextView) findViewById(R.id.title);
		title.setAlpha(0);
		mLayout = (PullPushLayout) this.findViewById(R.id.layout);
		mLayout.setOnTouchEventMoveListenre(new OnTouchEventMoveListenre() {

			@Override
			public void onSlideUp(int mOriginalHeaderHeight, int mHeaderHeight) {
				
			}

			@Override
			public void onSlideDwon(int mOriginalHeaderHeight, int mHeaderHeight) {
				
			}

			@Override
			public void onSlide(int alpha) {
				int alphaReverse = alphaMax - alpha;
				if (alphaReverse < 0) {
					alphaReverse = 0;
				}
				rl_title.getBackground().setAlpha(alpha);
				title.setAlpha(alpha);
			/*	bgNavBarDrawable.setAlpha(alpha);
				bglineNavBarDrawable.setAlpha(alpha);*/

			}
		});
		findViewById(R.id.duobao).setOnClickListener(this);
		findViewById(R.id.jieshao).setOnClickListener(this);
		findViewById(R.id.guize).setOnClickListener(this);
		findViewById(R.id.indianarecords).setOnClickListener(this);
		findViewById(R.id.thelottery).setOnClickListener(this);
		findViewById(R.id.shaishouqi).setOnClickListener(this);
		findViewById(R.id.kaijiangguocheng).setOnClickListener(this);
		name = (TextView) findViewById(R.id.name);
		content = (TextView) findViewById(R.id.content);
		qishu = (TextView) findViewById(R.id.qishu);
		zongxu = (TextView) findViewById(R.id.zongxu);
		jindu = (TextView) findViewById(R.id.jindu);
		
		xingyunma = (TextView) findViewById(R.id.xingyunma);
		zhongjiangzhe = (TextView) findViewById(R.id.zhongjiangzhe);
		huafei = (TextView) findViewById(R.id.huafei);
		
		httpclient = new HttpClients(this);
		httpclient.addResponseListener(this);
		  RequestParams params = new RequestParams();
		  params.add("id",id);
		httpclient.httpclient(ApiInterface.YIJIEXIAO, params, true);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.content:
			Toast.makeText(this,"0111", 1).show();
			break;
		case R.id.jieshao:
			intent = new Intent(YiJieXiao.this,XiJieWebView.class);
				intent.putExtra("codeGoodsID",codeGoodsID);
			startActivity(intent);
			
			break;
		case R.id.duobao:
			intent = new Intent(this,GouMai.class);
			
			startActivityForResult(intent, 1);
			this.overridePendingTransition(R.anim.push_buttom_in,R.anim.push_buttom_out);
			break;
	
		case R.id.guize:
			intent = new Intent(this,MyWebView.class);
			intent.putExtra("title","详情");
			startActivity(intent);
			
			break;
		case R.id.indianarecords:
			intent = new Intent(this,Indianarecords.class);
			intent.putExtra("codeGoodsID",codeGoodsID);
			intent.putExtra("codePeriod",codePeriod);
			startActivity(intent);
			
			break;
		case R.id.thelottery:
			intent = new Intent(this,Thelottery.class);
			intent.putExtra("codeGoodsID",codeGoodsID);
			startActivity(intent);
			
			break;
		case R.id.shaishouqi:
			intent = new Intent(this,ShaiShouQi.class);
			intent.putExtra("codeGoodsID",codeGoodsID);
			startActivity(intent);
			
			break;
		case R.id.kaijiangguocheng:
			intent = new Intent(this,LotteryProcess.class);
			//intent.putExtra("codeGoodsID",codeGoodsID);
			startActivity(intent);
			break;
		}
	}
	@Override
	public void myonScroll(int firstVisibleItem) {
		// TODO Auto-generated method stub
		if (firstVisibleItem == 0) {
			// ��ȡͷ����
			View views = listview.getChildAt(0);
			if (views != null) {
				// ��ȡͷ�������ڵ����ϲ���λ�õ��෴��
				int top = -views.getTop();
				// ��ȡͷ���ֵĸ߶�
				headerHeight = views.getHeight();
				// �������������ʱ����ͷ������XListview���������һ���ؼ���ʱ��ֻ�����ʱ�����ǲŵ���͸����
				if (top <= headerHeight && top >= 0) {
					
					// ��ȡ��ǰλ��ռͷ���ָ߶ȵİٷֱ�
					float f = (float) top / (float) headerHeight;
					int s = (int) (f * 255);
					rl_title.getBackground().setAlpha(s);
					// ֪ͨ������ˢ����ʾ
					rl_title.invalidate();
					
					
					title.setAlpha(s);
					title.invalidate();
					/*top_bg.setAlpha(1-f);
					
					titles.setVisibility(View.GONE);*/
				}
				if(top+dip2px(this, 45)>=headerHeight){
					//titles.setVisibility(View.VISIBLE);
					rl_title.getBackground().setAlpha(255);
					title.setAlpha(255);
				}
			}
		}else if (firstVisibleItem > 0) {
			rl_title.getBackground().setAlpha(255);
			title.setAlpha(255);
			
		} else {
			rl_title.getBackground().setAlpha(0);
			title.setAlpha(0);
		}
	}

	

    private int dip2px(Context context,float dpValue){  
        float scale = context.getResources().getDisplayMetrics().density;  
        return (int)(dpValue*scale +0.5f);  
    }
	@Override
	public void OnMessageResponse(String url, JSONObject jo)
			throws JSONException {
		
		if (url.equals(ApiInterface.YIJIEXIAO)) {
			if (null != jo) {
				slideshowView.gundongimg(jo.getJSONArray("Rows1")); 
				final JSONObject Rows2 = jo.getJSONObject("Rows2");
				name.setText(Rows2.getString("goodsName"));
				content.setText(Rows2.getString("goodsAltName"));
				codePeriod = Rows2.getString("codePeriod");
				
				qishu.setText("第"+codePeriod+"期夺宝");

				zongxu.setText("截止"+Rows2.getString("codeRTime"));
				jindu.setText(Rows2.getString("codeQuantity")+"次夺宝机会已被抢光，用时"+Rows2.getString("use_time"));
			
				codeGoodsID = Rows2.getString("codeGoodsID");
				
				xingyunma.setText(Rows2.getString("userWeb"));
				zhongjiangzhe.setText(Rows2.getString("userName"));
				huafei.setText("￥"+Rows2.getString("codeRUserBuyMoney"));
				
			}
		}
		
	}  
}
